package de.wbh.gwt.form.layouter.java.server.servlet.rpc;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.gwt.rpc.server.RpcServlet;

import de.wbh.gwt.form.layouter.gwt.rpc.FieldService;
import de.wbh.gwt.form.layouter.java.server.ConfigurationHelper;

public class FieldServiceImpl extends RpcServlet implements FieldService {

	private static String dir = ConfigurationHelper.getInstance().getHomeDirectory();

	@Override
	public List<String> getFieldIds(String efdName) {
		File f = new File(dir + "\\efd\\" + efdName + ".efd");
		try {
			String fileContent = Files.toString(f, Charsets.UTF_8);
			return Lists.newArrayList(Splitter.on(",").split(fileContent));
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public void updateEfd(String efdName, List<String> fieldIds) {
		File f = new File(dir + "\\efd\\" + efdName + ".efd");
		try {
			Files.write(Joiner.on(",").join(fieldIds), f, Charsets.UTF_8);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
